package com.epsoft.odin7cj.flow.api.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.camunda.bpm.engine.FormService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.epsoft.odin7cj.core.dto.RetData;
import com.epsoft.odin7cj.flow.bpo.form.FormBPO;
import com.epsoft.odin7cj.flow.bpo.task.TaskBPO;
import com.epsoft.odin7cj.flow.enums.actflow.FormAuditResultEnum;
import com.epsoft.odin7cj.flow.model.form.FixedForm;
import com.epsoft.odin7cj.flow.model.task.TaskInfo;

/**
 * DESC: 流程任务控制类
 * @author 胡义振  
 * @date 2020年06月30日
 */
@RestController
@RequestMapping("/api/flow/task")

public class TaskApi {
	
	@Autowired
	private TaskService taskService;
	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	private TaskBPO taskBPO;
	@Autowired
	private FormBPO formBPO;
	
    /**
     * DESC: 流程处理
     * @auther 胡义振
     * @date 2020年6月30日
     * @param taskId 任务ID
     * @param fixedForm   固定表单
     * @param dynamicFormData 动态表单
     * @param extFormData 外部表单
     * @return
     * @throws Exception 
     * @return
     */
	@RequestMapping("/deal")
    public RetData<String> deal(String taskId,FixedForm fixedForm,String dynamicFormData,String extFormData) throws Exception {
		
		RetData<String> retData = new RetData<String>();
		
		Map<String, Object> paramMap = formBPO.getFormData(fixedForm, dynamicFormData, extFormData);
		
		if(paramMap!=null) {
			for(Map.Entry<String, Object> entry : paramMap.entrySet()) {
				taskService.setVariableLocal(taskId, entry.getKey(), entry.getValue());
			}
		}
		
		// 这里既用 setVariableLocal 又用 complete(taskId,paramMap) 目的是为了在流程变量表中，会把 TASK_ID 值放进去
		// 审核通过
		if(FormAuditResultEnum.AUDIT_RESULT_1.getCode().equals(fixedForm.getAuditResult()) || fixedForm.getAuditResult()==null || "".equals(fixedForm.getAuditResult())) {
			taskService.complete(taskId,paramMap);
		}
		// 退回第一节点
		else if(FormAuditResultEnum.AUDIT_RESULT_2.getCode().equals(fixedForm.getAuditResult())) {
			taskBPO.returnFirstNode(paramMap, taskId);
		}
		// 退回上一节点
		else if(FormAuditResultEnum.AUDIT_RESULT_3.getCode().equals(fixedForm.getAuditResult())) {
			taskBPO.returnPreNode(paramMap, taskId);
		}
    	retData.setRetBody("");
    	return retData;
    }
    /**
     * Desc: 获取普通任务处理
     * @return
     * @throws Exception
     */
	@RequestMapping("/getTask")
    public RetData<List<TaskInfo>> getReformTask() throws Exception {
		RetData<List<TaskInfo>> retData = new RetData<List<TaskInfo>>();
		List<TaskInfo> listBizFlowTask = new ArrayList<TaskInfo>();
		List<Task> listTask = taskService.createTaskQuery().list();
		for(Task task : listTask) {
			// 查询流程名称
			ProcessDefinition singleResult = repositoryService.createProcessDefinitionQuery().processDefinitionId(task.getProcessDefinitionId()).singleResult();
			TaskInfo bizFlowTask = new TaskInfo();
			bizFlowTask.setTaskId(task.getId());
			bizFlowTask.setTaskName(task.getName());
			if(singleResult!=null) {
				bizFlowTask.setFdName(singleResult.getName());
			}
			bizFlowTask.setTaskDefinitionKey(task.getTaskDefinitionKey());
			bizFlowTask.setTaskOwner(task.getOwner());
			bizFlowTask.setInstanceId(task.getProcessInstanceId());
			bizFlowTask.setFdId(task.getProcessDefinitionId());
			listBizFlowTask.add(bizFlowTask);
		}
    	retData.setRetBody(listBizFlowTask);
    	return retData;
    }
	

	
	
	
}
